//
//  BudgetManagerViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 15.04.25.
//

import Foundation
class BudgetManagerViewModel : KoiAccountingBaseViewModel {
    
    private let budgetRepo = BudgetRepo()
    
    @Published var selectedCategory: AccountingCategory? = nil
    @Published var selectedBudget: AccountingBudget? = nil
    
    @Published var budgetTotal: AccountingBudget? = nil
    @Published var budgetTotalAmount: Double = 0.0
    @Published var expenseAmount: Double = 0.0
    @Published var surplusAmount: Double = 0.0
    @Published var budgetList: [BudgetCombine] = [BudgetCombine]()
    
    
    @Published var saveCategoryBudgetState = ""
    @Published var saveTotalBudgetState = ""
    @Published var deleteAllBudgetState = ""
    
    func getBudgetCategoryList() -> [AccountingCategory]{
        return budgetList.map { $0.category }
    }
    
    func loadData() {
        getBudgetTotal()
        getBudgetCategory()
    }
    
    func getBudgetTotal() {
        asyncBackground {
            let result = self.budgetRepo.getBudgetTotal(month: Date().toYearMonth(), uid: self.uid, bookId: self.bookId)
            self.asyncMain {
                self.budgetTotalAmount = result.budgetAmount
                self.expenseAmount = result.expenseAmount
                self.surplusAmount = result.surplusAmount
                self.budgetTotal = result.budgetTotal
            }
        }
        
    }
    
    func getBudgetCategory() {
        asyncBackground {
            let result = self.budgetRepo.getBudgetCategory(uid: self.uid, bookId: self.bookId)
            self.asyncMain {
                self.budgetList = result
            }
        }
    }
    
    func saveBudgetTotal(inputAmount: String) {
        if budgetTotal == nil {
            // 新增预算
            createBudget(inputAmount: inputAmount)
        } else {
            // 更新预算
            updateBudget(budget: budgetTotal, inputAmount: inputAmount)
        }
    }
    
    private func createBudget(inputAmount: String) {
        asyncBackground {
            let result = self.budgetRepo.save(inputAmount: inputAmount, uid: self.uid, bookId: self.bookId)
            
            self.asyncMain {
                switch result {
                case .success:
                    self.saveTotalBudgetState = "success"
                case .failure(let failure):
                    self.saveTotalBudgetState = failure.netErrorMsg()
                }
            }
        }
    }
    
    private func updateBudget(budget: AccountingBudget?, inputAmount: String) {
        guard var budget = budget else { return }
        asyncBackground {
            budget.amount = Double(inputAmount)
            let result = self.budgetRepo.update(budget: budget)
            
            self.asyncMain {
                switch result {
                case .success:
                    self.saveTotalBudgetState = "success"
                case .failure(let failure):
                    self.saveTotalBudgetState = failure.netErrorMsg()
                }
            }
        }
    }
    
    func saveBudgetCategory(inputAmount: String) {
        // 判断新增或更新
        if var budget = selectedBudget {
            // 更新
            asyncBackground {
                budget.amount = Double(inputAmount)
                let result = self.budgetRepo.update(budget: budget)
                self.asyncMain {
                    switch result {
                    case .success:
                        self.saveCategoryBudgetState = "success"
                    case .failure(let failure):
                        self.saveCategoryBudgetState = failure.netErrorMsg()
                    }
                }
            }
        } else {
            asyncBackground {
                let result = self.budgetRepo.save(inputAmount: inputAmount, uid: self.uid, bookId: self.bookId, category: self.selectedCategory!)
                self.asyncMain {
                    switch result {
                    case .success:
                        self.saveCategoryBudgetState = "success"
                    case .failure(let failure):
                        self.saveCategoryBudgetState = failure.netErrorMsg()
                    }
                }
            }
        }
    }
    
    // 删除全部预算
    func deleteAllBudget() {
        asyncBackground {
            let result = self.budgetRepo.deleteAllBudget(uid: self.uid, bookId: self.bookId)
            self.asyncMain {
                switch result {
                case .success:
                    self.deleteAllBudgetState = "success"
                case .failure:
                    self.deleteAllBudgetState = "fail"
                }
            }
        }
    }
}
